﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BandManager
{
    public partial class Form1 : Form
    {
        private NetworkAdapter[] adapters;
        private NetworkMonitor monitor;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            monitor = new NetworkMonitor();
            this.adapters = monitor.Adapters;
            /* If the length of adapters is zero, then no instance  
             * exists in the networking category of performance console.*/
            if (adapters.Length == 0)
            {
                this.ListAdapters.Enabled = false;
                MessageBox.Show("No network adapters found on this computer.");
                return;
            }
            this.ListAdapters.Items.AddRange(this.adapters);  
        }

        private void ListAdapters_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            monitor.StopMonitoring();
            // Start a timer to obtain new performance counter sample every second.  
            monitor.StartMonitoring(adapters[this.ListAdapters.SelectedIndex]);
            this.TimerCounter.Start();
        }

        private void TimerCounter_Tick(object sender, System.EventArgs e)
        {
            NetworkAdapter adapter = this.adapters[this.ListAdapters.SelectedIndex];
            /* The DownloadSpeedKbps and UploadSpeedKbps are double values. You can also  
             * use properties DownloadSpeed and UploadSpeed, which are long values but  
             * are measured in bytes per second. */
            //adapter.refresh();
            this.LableDownloadValue.Text = String.Format("{0:n} kbps", adapter.DownloadSpeedKbps);
            this.LabelUploadValue.Text = String.Format("{0:n} kbps", adapter.UploadSpeedKbps);
        } 
    }
}
